import re, json, base64
import requests
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad
import demjson3
# ---------- 全局常量 ----------
INDEX_URL   = 'https://spa7.scrape.center'

KEY         = b'fipFfVsZ'          # 8 字节 DES 密钥
HEADERS     = {
    'User-Agent': 'Mozilla/5.0',
    'Referer'   : INDEX_URL
}

# from Crypto.Cipher import DES
# from Crypto.Util.Padding import pad
# import base64
#
# key = b'fipFfVsZ'  # 8 字节
# def get_token(name, birthday, height, weight):
#     plain = base64.b64encode(name.encode()).decode() + birthday + height + weight
#     cipher = DES.new(key, DES.MODE_ECB)
#     return base64.b64encode(cipher.encrypt(pad(plain.encode(), 8))).decode()
#
# print(get_token('凯文-杜兰特', '1988-09-29', '208cm', '108.9KG'))
#
#
# token_str= "DG1uMMq1M7OeHhds71HlSMHOoI2tFpWCB4ApP00cVFqptmlFKjFu9RluHo2w3mUw"
#
# url = DETAIL_API + '?token=' + token_str
#
# response = requests.get(url, headers=HEADERS)
# print(response.status_code)
# print(response.text)
# ---------- 1. 拿球员列表 ----------
def fetch_players():
    js = requests.get(INDEX_URL + '/js/main.js', headers=HEADERS).text
    # 简单正则提取 players 数组
    m = re.search(r'const players\s*=\s*(\[.*?\])', js, re.S)
    print(m)
    if not m:
        raise RuntimeError('未找到 players 数组')
    return demjson3.decode(m.group(1))

# ------- 3. 拿详情并保存 ----------

def main():
    players = fetch_players()
    results = []
    for p in players:

        results.append(p)
        img_result = requests.get(INDEX_URL+'/img/' + p['image'], headers=HEADERS).content

        with open(f'./img/{p["name"]}.png', 'wb') as f:
            f.write(img_result)
            print(f'已保存{p["name"]}')

    with open('players.json', 'w', encoding='utf-8') as f:
        json.dump(results, f, ensure_ascii=False, indent=2)


if __name__ == '__main__':
    main()


